package client.fx;

import java.net.URL;
import java.util.ResourceBundle;

import javax.xml.stream.events.StartDocument;

import client.socket.Client;
import date.util.FormatDate;
import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

public class ClientController implements Initializable {

	@FXML
	private TextField userNameText=new TextField();//用户名
	@FXML
	private ListView<String> userListView=new ListView<String>();//在线用户列表
	@FXML
	private TextArea sendArea=new TextArea();//发送框
	@FXML
	private Button sendBtn=new Button();//发送按钮
	@FXML
	private TextArea receArea=new TextArea();//接收框
	@FXML
	private Button loginBtn=new Button();//登录按钮
	@FXML
	private Button logoutBtn=new Button();//退出按钮
	@FXML 
	private Text tipLabel=new Text();//提示框
	
	private Client client;//客户端后台
	
	private ObservableList<String> userList=FXCollections.observableArrayList();//用户列表
	
	private SimpleStringProperty receMsg=new SimpleStringProperty();//接收消息字符串
	
	private SimpleStringProperty userName=new SimpleStringProperty("  ");//用户名
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		client=new Client(userName,userList,receMsg);
		
	    userListView.setItems(userList);
	    
	    receMsg.addListener(e->{
	    	receArea.setText(receArea.getText()+receMsg.get()+"\n");
	    });
	    
	    userName.addListener(e->{
	    	userNameText.setText(userName.get());
	    });
	    
	    userList.addListener((ListChangeListener<String>)e->{
	    	if(userList.isEmpty()) {
	    		sendBtn.setDisable(true);
	    	}else {
	    		sendBtn.setDisable(false);
	    	}
	    });
	    
	}

	/**
	 * 发送按钮事件
	 * @param e
	 */
	@FXML
	public void sendBtnOnAction(ActionEvent e) {
		String user=userListView.getSelectionModel().getSelectedItem();
		String msg=sendArea.getText();
		new Thread() {
			public  void run() {		
				client.sendToUser(user,msg);
			}
			
		}.start();
		sendArea.setText("");
		receArea.setText(receArea.getText()+FormatDate.getDate()+"  To:"+user+"\n"+msg+"\r\n\n");
	}
	
	/**
	 * 登录按钮事件
	 * @param e
	 */
	@FXML
	public void loginBtnOnAction(ActionEvent e) {
		
		new Thread() {
			public void run() {
				if(client.login()) {
					loginBtn.setDisable(true);
					logoutBtn.setDisable(false);
					tipLabel.setText("已登录...");
				}
				
			}
		}.start();
	}
	
	/**
	 * 退出按钮事件
	 * @param e
	 */
	@FXML 
	public void logoutBtnOnAction(ActionEvent e) {
		
	    new Thread() {
		   public void run() {
			   client.logout();
			   loginBtn.setDisable(false);
				logoutBtn.setDisable(true);
				tipLabel.setText("未登录...");
		   }
	   }.start();
	}

}
